<?php
load_ctl("Common");
class MessageController extends CommonController
{ 
	function init(){
		parent::init();
		load_model('Message');
		$this->message = new Message();
    }
	public function indexAction(){
		$page = isset($_GET["p"]) ? intval($_GET["p"]) : 1;
	    $page < 1 && $page = 1;
		$page_size = 10;
		$this->view->mes_list = $this->message->get_message_list();
		$this->view->webTitle = '访客留言 - ';
		$this->view->total_mes = count($this->view->mes_list);
		
		$paginator = Zend_Paginator::factory($this->view->mes_list); 
		$paginator->setCurrentPageNumber($page);
		$paginator->setItemCountPerPage($page_size); 
		$this->view->paginator = $paginator;
		echo $this->view->render('message.phtml');        
	}

	public function addmesAction(){
		if($this->_request->isPost()){
			$filter = new Zend_Filter_StripTags();
			if(trim($this->_request->getPost('mes_writer')) == ''){
				php_alert("用户名不能为空",-1);
				exit;
			}elseif(trim($this->_request->getPost('mes_content')) == ''){
				php_alert("内容不能为空",-1);
				exit;
			}else if($this->_request->getPost('verify')=='' || $this->_request->getPost('verify') != $_SESSION['authcode']){
				php_alert("验证码出错",-1);
				exit;
			}
			$mes_ip = GetIP();
			$data = array(
				'mes_writer'=>$filter->filter($this->_request->getPost('mes_writer')),
				'mes_content'=>$filter->filter($this->_request->getPost('mes_content')),
				'mes_date'=>time(),
				'mes_ip'=>$mes_ip,
			);
			$this->message->add_mes($data);
			php_alert("留言发表成功",$this->view->baseUrl.'/message');
		}else{
			$this->_redirect('/');
		}
	}
	
	public function noRouteAction(){
		$this->_redirect('/');
	}
}
?>